package org.example.stockdemo.job;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.example.stockdemo.entity.StockDetail;
import org.example.stockdemo.entity.StockIndex;
import org.example.stockdemo.service.StockDetailService;
import org.example.stockdemo.service.StockGroupItemService;
import org.example.stockdemo.service.StockIndexService;
import org.example.stockdemo.service.TencentStockService;
import org.example.stockdemo.util.BeanExtUtil;
import org.example.stockdemo.util.CacheUtil;
import org.example.stockdemo.util.StockUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author :Mr.zhou
 * @description: 定时刷新股票数据任务
 * @create date: 2025/10/24 22:43
 */
@Slf4j
@Service
public class StockRealFlushJob {

    private static String indexPercentStr;
    @Autowired
    private StockGroupItemService stockGroupItemService;
    @Autowired
    private StockIndexService stockIndexService;
    @Autowired
    private TencentStockService tencentStockService;

    /**
     * 定时刷新股票数据
     * 每天上午9:30到11:30期间，每隔10秒执行一次
     */
    @Scheduled(cron = "0/15 * 9-11 * * ?")
    @Scheduled(cron = "0/15 * 13-23 * * ?")
    public void flushStockData() {
        try {
            if (CacheUtil.isFlush && StockUtil.isTradingTime()) {
                List<StockDetail> detailList = stockGroupItemService.getAndCacheStocks(1L);
                printStockDatas(detailList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("刷新数据失败:{}", e.getMessage());
        }
    }


    /**
     * 刷新指数
     */
    @Scheduled(cron = "0 * 9-11 * * ?")
    @Scheduled(cron = "0 * 13-14 * * ?")
    public void flushSaveIndex() {
        if (CacheUtil.isFlush && StockUtil.isTradingTime()) {
            List<StockDetail> stockRealtimeQuotes = tencentStockService.getBatchRealtimeQuotes(CacheUtil.indexCodes, false);
            List<StockIndex> stockIndexList = BeanExtUtil.copyList(stockRealtimeQuotes, StockIndex.class);
            if (CollectionUtils.isNotEmpty(stockIndexList)){
                // 沪深成交额与成交量合并
                StockIndex shIndex = stockIndexList.get(0);
                shIndex.setAmount(shIndex.getAmount()+stockIndexList.get(1).getAmount());
                shIndex.setVolume(shIndex.getVolume()+stockIndexList.get(1).getVolume());
                stockIndexList.remove(1);
                stockIndexService.saveBatch(stockIndexList);
                // 遍历集合，将所有元素的名称和百分比拼接起来
                indexPercentStr = stockRealtimeQuotes.stream()
                        .map(stockDetail -> stockDetail.getName() + "(" + stockDetail.getChangePercent() + ") ")
                        .collect(Collectors.joining(" "));
            }
        }
    }

    private void printStockDatas(List<StockDetail> stockDetails) {
        System.out.println("--------------" + indexPercentStr + "-----------------");
        for (StockDetail stockDetail : stockDetails) {
            // 每打印5条数据，换行
            int index = stockDetails.indexOf(stockDetail);
            if (index % 5 == 0 && index != 0) {
                System.out.println();
            }
            System.out.print(stockDetail.getName() + "(" + stockDetail.getPrice() + " " + stockDetail.getChangePercent() + " ) \t");
        }
        System.out.println();
    }


}